package tw.asap.plus.ui.custom;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import tw.asap.plus.R;

/**
 * Description:
 * Created:      [15/11/12]
 * Author:       [Yang]
 **/
public class QuantityController extends FrameLayout {
    private final Context context;
    @InjectView(R.id.bt_minus)
    ImageView btMinus;
    @InjectView(R.id.tv_num)
    TextView tvNum;
    @InjectView(R.id.bt_plus)
    ImageView btPlus;

    QuantityChangeListener addListeners;
    QuantityChangeListener minusListener;

    int currentQuantity = 0;

    public QuantityController(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public QuantityController(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.view_num_control, this);
        ButterKnife.inject(this);
        tvNum.setVisibility(View.GONE);
        btMinus.setVisibility(View.GONE);
    }

    public QuantityController(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public QuantityController(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        initView();
    }

    @OnClick(R.id.bt_plus)
    protected void clickPlus() {
        if (currentQuantity == 0) {
            btMinus.setVisibility(View.VISIBLE);
            tvNum.setVisibility(View.VISIBLE);
        }
        currentQuantity++;
        tvNum.setText(String.valueOf(currentQuantity));
        if (addListeners != null) {
            addListeners.onChange(currentQuantity);
        }
    }

    @OnClick(R.id.bt_minus)
    protected void clickMinus() {
        if (currentQuantity == 1) {
            btMinus.setVisibility(View.GONE);
            tvNum.setVisibility((View.GONE));
        }
        currentQuantity--;
        tvNum.setText(String.valueOf(currentQuantity));
        if (minusListener != null) {
            minusListener.onChange(currentQuantity);
        }
    }


    public void reset() {
        currentQuantity = 0;
        tvNum.setText(String.valueOf(currentQuantity));
        btMinus.setVisibility(View.GONE);
        tvNum.setVisibility((View.GONE));
    }

    public void setStatus(int count){
        currentQuantity = count;
        if (currentQuantity == 0) {
            btMinus.setVisibility(View.GONE);
            tvNum.setVisibility((View.GONE));
        } else {
            btMinus.setVisibility(View.VISIBLE);
            tvNum.setVisibility((View.VISIBLE));
            tvNum.setText(String.valueOf(currentQuantity));
        }
    }

    public int getCount(){
        return currentQuantity;
    }


    public void setOnMinusListener(QuantityChangeListener listener) {
        this.minusListener = listener;
    }

    public void setOnAddListener(QuantityChangeListener listener) {
        this.addListeners = listener;
    }

    public interface QuantityChangeListener {
        void onChange(int newQuantity);
    }

}
